package Cantella::DJabberd::Web::Controller::Site;

use base 'Reaction::UI::Controller::Root';
use Reaction::Class;

use aliased 'Reaction::UI::ViewPort';
use aliased 'Cantella::DJabberd::Web::UI::ViewPort::Static';
use aliased 'Cantella::DJabberd::Web::UI::ViewPort::Layout';
use aliased 'Cantella::DJabberd::Web::UI::ViewPort::SidebarItem';

#
# Sets the actions in this controller to be registered with no prefix
# so they function identically to actions created in MyApp.pm
#
__PACKAGE__->config(
  view_name => 'Site',
  window_title => 'Cantella Web',
  namespace => ''
);

sub base :Chained('/') :PathPart('') :CaptureArgs(0) {
  my ($self, $c) = @_;
  my $layout = $c->stash->{layout} = $self->push_viewport(Layout);
  push @{$layout->stylesheets}, '/css/componentui-basic.css';
  my $menu    = $c->stash->{menu}  = $layout->menu;
  my $submenu = $c->stash->{submenu} = $layout->submenu;
  my $sidebar = $c->stash->{sidebar} = $layout->sidebar;
  $self->build_nav($c);
  return;
}

sub root :Chained('base') :PathPart('') :Args(0) {
  my ($self, $c) = @_;
  $c->stash->{menu}->current_item('home');
  $self->push_viewport(Static, file_name => 'index');
  unless ($c->stash->{layout}->has_inner){
    $c->log->debug("Where's inner?");
  }
}

sub static :Chained('base') :PathPart('css') :Args {
  my ($self, $c, @args) = @_;
  return if $c->stash->{window}->view->serve_static_file($c, \@args);
  $c->forward('error_404');
}

sub build_nav {
  my($self, $c) = @_;
  my $menu = $c->stash->{menu};
  $menu->add_item('home', uri => $c->uri_for('/') );
  if ( $c->has_current_user ) {
    my $user = $c->current_user;
    if ( $user->check_role('Administrator')) {
      $menu->add_item('admin', uri => $c->uri_for('/admin') );
    }
  }

}

1;
